class BookController {
  // 控制器
  async index(ctx) {
    console.log("书籍控制器 url + data 方式");
    ctx.body = await ctx;
  }

  // 列表
  async find(ctx) {
    await new Promise(resolve => setTimeout(resolve, 1 * 1000));

    let arr = Array.from({ length: 10 }, (_, i) => i);
    ctx.body = arr;
  }

  // 添加
  async add(ctx) {
    await new Promise(resolve => setTimeout(resolve, 1 * 1000));

    let arr = Array.from({ length: 10 }, (_, i) => i);
    arr.push(ctx.data.value);
    ctx.body = arr;
  }

  // 修改
  async update(ctx) {
    await new Promise(resolve => setTimeout(resolve, 1 * 1000));

    const id = Number(ctx.query.id);
    if (isNaN(id) || id < 0) {
      throw "修改下标参数错误";
    }
    let arr = Array.from({ length: 10 }, (_, i) => i);
    arr[id] = ctx.data.value;
    ctx.body = arr;
  }

  // 删除
  async remove(ctx) {
    await new Promise(resolve => setTimeout(resolve, 1 * 1000));

    const id = Number(ctx.query.id);
    if (isNaN(id) || id < 0) {
      throw "删除下标参数错误";
    }
    let arr = Array.from({ length: 10 }, (_, i) => i);
    arr.splice(id, 1);
    ctx.body = arr;
  }
}

module.exports = new BookController();
